// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.database.parts;

import org.dengues.commons.ui.gef.SimpleHtmlFigure;
import org.dengues.commons.utils.DenguesTextUtil;
import org.dengues.designer.ui.database.policies.DiagramChildDirectEditPolicy;
import org.dengues.designer.ui.editors.editmanager.DiagramChildCellEditorLocator;
import org.dengues.designer.ui.editors.editmanager.DiagramChildDirectEditManager;
import org.dengues.model.database.ForeignKeyLabel;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.jface.viewers.TextCellEditor;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-20 qiang.zhang $
 * 
 */
public class ForeignKeyLabelEditPart extends AbstractDBGraphicalEditPart {

    private SimpleHtmlFigure htmlFig;

    private DiagramChildDirectEditManager directEditManager;

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
     */
    @Override
    protected IFigure createFigure() {
        createNodeParameters();
        String text = getCastModel().getName();
        htmlFig = new SimpleHtmlFigure();
        htmlFig.setText(text);
        return htmlFig;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
     */
    @Override
    protected void createEditPolicies() {
        installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new DiagramChildDirectEditPolicy());
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#refreshVisuals()
     */
    @Override
    protected void refreshVisuals() {
        Point textOffset = new Point(getCastModel().getX(), getCastModel().getY());
        Dimension size = htmlFig.getPreferredSize();
        htmlFig.setText(DenguesTextUtil.getNotNullString(getCastModel().getName()));
        Rectangle rectangle = new Rectangle(textOffset, size);
        ((GraphicalEditPart) getParent()).setLayoutConstraint(this, getFigure(), rectangle);
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
     */
    public void notifyChanged(Notification notification) {
        refresh();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.dengues.designer.ui.database.parts.AbstractDBGraphicalEditPart#getCastModel()
     */
    @Override
    public ForeignKeyLabel getCastModel() {
        return (ForeignKeyLabel) getModel();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.editparts.AbstractEditPart#performRequest(org.eclipse.gef.Request)
     */
    @Override
    public void performRequest(Request req) {
        if (req.getType() == RequestConstants.REQ_DIRECT_EDIT) {
            if (directEditManager == null) {
                directEditManager = new DiagramChildDirectEditManager(this, TextCellEditor.class,
                        new DiagramChildCellEditorLocator(getFigure(), -2, -2, 4, 4));
            }
            directEditManager.show();
        }
        super.performRequest(req);
    }

}
